package com.hp.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.hp.ssm.entity.Files;
import com.hp.ssm.entity.Patient;
import com.hp.ssm.service.PatientService;
import com.hp.ssm.util.GeneratID;
import com.hp.ssm.util.ResultJson;
import com.hp.ssm.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/patient")
public class PatientController {
    @Autowired
    private PatientService PatientService;
    //可以操作的列表
    @RequestMapping("/showAll")
    @ResponseBody
    public void showAll(Patient patient, Integer page, Integer limit, HttpServletResponse resp) {
        System.out.println();
        List<Patient> list = PatientService.showAll(page,limit,patient);
        PageInfo<Patient> userPageInfo = new PageInfo<Patient>(list);
        System.out.println(userPageInfo + "----------------------------------------------");
        ResultJson success = ResultJson.success(userPageInfo);
        Utils.returnJson(success, resp);
    }

    //假删除的列表
    @RequestMapping("/showhospital2")
    @ResponseBody
    public ModelAndView showAll2(Patient patient,HttpServletResponse resp, Integer page, Integer limit) {
        System.out.println(patient);
        ModelAndView mv = new ModelAndView();
        List<Patient> list = PatientService.showAll2(page,limit,patient);
        PageInfo<Patient> userPageInfo = new PageInfo<Patient>(list);
        System.out.println(list + "----------------------------------------------");
        ResultJson success = ResultJson.success(userPageInfo);
        Utils.returnJson(success, resp);
        return mv;
    }

    @ResponseBody
    @RequestMapping("/findByid")
    public void findByid(int pid,HttpServletResponse resp) {
        Patient p = PatientService.findId(pid);
        System.out.println(p + "<<<<<<<<<id>>>>>>>>>");
        ResultJson success = ResultJson.success(p);
        Utils.returnJson(success, resp);
    }


    @ResponseBody
    @RequestMapping("/insert")
    public void insert(Patient patient, HttpServletResponse resp) throws Exception {
        System.out.println(patient);
        Date ptime = new Date();
        Files files =new Files();
        Date ctime = new Date();
        String  puser = GeneratID.getGeneratID();
        String cname = patient.getFiles().getCname();
        Integer uposition = patient.getFiles().getUposition();
        files.setCtime(ctime);
        files.setCname(cname);
        files.setUposition(uposition);
        patient.setPuser(puser);
        patient.setPtime(ptime);
        patient.setFiles(files);
        int i = PatientService.insert(patient);
        if(i > 0){
            String pphone = patient.getPphone();
            int uid = PatientService.uid(pphone);
            files.setUid(uid);
        }
        int a = PatientService.add(patient);
        System.out.println(i + "----------222--------");
        if(a > 0) {
            ResultJson success = ResultJson.success(i);
            Utils.returnJson(success, resp);
        }else {
            ResultJson error = ResultJson.error();
            Utils.returnJson(error, resp);
        }
    }
    @ResponseBody
    @RequestMapping("/update")
    public void update(Patient patient, HttpServletResponse resp){
        System.out.println(patient);
        int i = PatientService.updateStatus(patient);
        System.out.println(i + "----------111--------");
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success, resp);
    }
    @ResponseBody
    @RequestMapping("/del")
    public void del(Integer pid, HttpServletResponse resp){
        int i = PatientService.fakedel(pid);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success, resp);
    }
    @ResponseBody
    @RequestMapping("/recover")
    public void recover(Integer pid, HttpServletResponse resp){
        int i = PatientService.recover(pid);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success, resp);
    }
}
